﻿Public Class SuperTextBox
    Inherits Windows.Forms.TextBox

    Private m_blnIsDecimal As Boolean
    Private m_blnOnlyNumber As Boolean
    Private oTooltip As New Windows.Forms.ToolTip

    Public Property IsPossibleSpacing() As Boolean
    Public Property IsDecimal() As Boolean
        Get
            Return m_blnIsDecimal
        End Get
        Set(ByVal value As Boolean)
            m_blnIsDecimal = value
            If value Then
                OnlyNumber = True
            End If
        End Set
    End Property
    Public Property OnlyNumber() As Boolean
        Get
            Return m_blnOnlyNumber
        End Get
        Set(ByVal value As Boolean)
            m_blnOnlyNumber = value
            If value Then
                Me.TextAlign = Windows.Forms.HorizontalAlignment.Right
            Else
                Me.TextAlign = Windows.Forms.HorizontalAlignment.Left
            End If
        End Set
    End Property
    Public Property ToolTip() As String
        Get
            Return oTooltip.GetToolTip(Me)
        End Get
        Set(ByVal value As String)
            oTooltip.SetToolTip(Me, value)
            oTooltip.ShowAlways = True
        End Set
    End Property

#Region "Funzioni Publiche"

    Public Sub SetFocusSelected()
        Me.SelectAll()
        Me.Focus()
    End Sub

    Public Function GetIntNumber() As Integer
        If IsNumeric(Me.Text) Then
            GetIntNumber = CInt(Me.Text)
        Else
            GetIntNumber = 0
        End If
    End Function

    Public Function GetDecimal() As Double
        If IsNumeric(Me.Text) Then
            GetDecimal = CDbl(Me.Text)
        Else
            GetDecimal = 0
        End If
    End Function

    Public Function IsCorrect() As Boolean
        If OnlyNumber Then
            If IsNumeric(Me.Text) Then
                IsCorrect = True
            Else
                IsCorrect = False
            End If
        Else
            IsCorrect = True
        End If
    End Function

#End Region

#Region "Funzioni Private"

    Private Function ControlloNumerico(ByVal inNumero As String) As Boolean
        Dim i As Integer
        Dim intNumeroOccorrenze As Integer

        If InStr(1, inNumero, ",", vbTextCompare) = 0 Then
            ControlloNumerico = True
        Else
            For i = 1 To Len(inNumero)
                If Mid(inNumero, i, 1) = "," Then
                    intNumeroOccorrenze = intNumeroOccorrenze + 1
                End If
            Next i

            If intNumeroOccorrenze > 1 Then
                ControlloNumerico = False
            Else
                If InStr(1, inNumero, ",", vbTextCompare) = 1 Or InStr(1, inNumero, ",", vbTextCompare) = Len(inNumero) Then
                    ControlloNumerico = False
                Else
                    ControlloNumerico = True
                End If
            End If
        End If
    End Function

#End Region

#Region "Funzioni Eventi"

    Private Sub me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Windows.Forms.Keys.Return
                If Not Me.Multiline And e.Handled = False Then
                    e.Handled = True
                    Windows.Forms.SendKeys.Send("{TAB}")
                    e.SuppressKeyPress = True
                End If
            Case Windows.Forms.Keys.Escape
                e.SuppressKeyPress = True
        End Select
    End Sub

    Private Sub txtdati_Validate(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
        e.Cancel = False
        If Not e.Cancel Then
            If IsDecimal Then
                If Me.Text <> "" Then
                    e.Cancel = Not ControlloNumerico(Me.Text)
                Else
                    e.Cancel = False
                End If
                If e.Cancel Then
                    MsgBox("Insert correct numeric value.", vbCritical, "Warning")
                End If
            Else
                e.Cancel = False
            End If
        End If
    End Sub

    Private Sub txtdati_LostFocus() Handles Me.LostFocus
        Me.BackColor = Drawing.Color.White
    End Sub

    Private Sub txtdati_GotFocus() Handles Me.GotFocus
        Me.BackColor = Drawing.Color.LemonChiffon
    End Sub

    Private Sub me_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Select Case e.KeyChar
            Case " "
                If OnlyNumber And Not IsPossibleSpacing Then
                    Beep()
                    e.KeyChar = ""
                End If
            Case "."
                If IsDecimal Then
                    e.KeyChar = ","
                End If
            Case Else
                If Not (IsNumeric(e.KeyChar) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 13) Then
                    If OnlyNumber Then
                        Beep()
                        e.KeyChar = ""
                    End If
                End If
        End Select

    End Sub

#End Region

End Class
